$(document).ready(function () {

	$.ajaxSetup({
		headers: {
			'X-CSRF-Token': $('meta[name="csrf-token"]').attr('content')
		}
	});
	
	if($('#summernote').length > 0) {
		$('#summernote').summernote();
	}

	if($("#titre").length > 0 && $("#permalien").length > 0) {
		$("#titre").keyup(function() {
			$('#permalien').val(generatePermalink($(this).val()));
		});
	}

	$("#news-form").submit(function() {
		$("#contenu").val($("#summernote").code());
	});

	$("#bg-form").submit(function(){
		$("#texte").val($("#summernote").code());

		$("#url").val(
			generatePermalink($("#titre").val())		
		);
	});

	$(".delete-file").each(function() {
		$(this).click(function(e) {
			e.preventDefault();
			var location = $(this).attr('href');
			 bootbox.confirm("Supprimer le fichier "+$(this).data('name')+" ?", function(result) {
				if(result) {
				   window.location.replace(location);
				}
			});
		}); 
	});

	$("#searchc").keyup(function(e) {
		$("#char-list > tbody").html("");
		$.post(BASE_URL+"/search-characters.html", { searchc: $("#searchc").val() },
			function(data){
				$("#char-list > tbody").html(data);
			}, "html"
		);
	});

	$("#searchu").keyup(function(e) {
		if(e.keyCode == 13) {
			$("div.pagination").remove();
			$.post(BASE_URL+"/search-users.html", { searchu: $("#searchu").val() },
				function(data){
					$("table.table > tbody").html(data);
				}, "html"
			);
		}
	});

	if($('#datepick').length > 0) {
		$("#datepick").appendDtpicker({
			'locale': 'fr',
			'closeOnSelected': true,
			'minuteInterval': 15
		});
	}

	$(document).on('click', '.add-member', function() {
		var name = $.trim($(this).parent().prev().text());
		var html = '<input class="form-control" name="characters[]" type="text" value="'+name+'">';
		$(".character-choice > label").after(html);

		$("#searchc").val("");
	});
});

function trim(str, chars) {
	return ltrim(rtrim(str, chars), chars);
}

function ltrim(str, chars) {
	chars = chars || "\\s";
	return str.replace(new RegExp("^[" + chars + "]+", "g"), "");
}

function rtrim(str, chars) {
	chars = chars || "\\s";
	return str.replace(new RegExp("[" + chars + "]+$", "g"), "");
}

function generatePermalink(value) {
	permalien = value.replace(/[àâä]/gi,"a");
	permalien = permalien.replace(/[éèêë]/gi,"e");
	permalien = permalien.replace(/[îï]/gi,"i");
	permalien = permalien.replace(/[ôö]/gi,"o");
	permalien = permalien.replace(/[ùûü]/gi,"u");
	permalien = permalien.replace(/[ç]/gi,"c");
	permalien = permalien.replace(/[/]/gi,"-");
	permalien = permalien.replace(/[()!?,;*$#€%+.|&><=@~©™]/gi,"");
	permalien = permalien.replace(/[-]/gi,"");
	permalien = permalien.replace(/[:]/gi,"");
	permalien = permalien.replace('^',"");
	permalien = permalien.replace('{',"");
	permalien = permalien.replace('}',"");
	permalien = permalien.replace("'","-");
	permalien = permalien.replace('"',"");
	permalien = permalien.replace('[',"");
	permalien = permalien.replace(']',"");
	permalien = permalien.replace(/ /gi,"-");
	permalien = permalien.toLowerCase();
	permalien = trim(permalien,'-');
	return permalien;
}
